﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Messaging;
using System.ComponentModel.Composition;

namespace Compose
{
    //[PartCreationPolicy(CreationPolicy.NonShared)]
    public class CmdLine : ICommand
    {
        /// <summary>
        /// 通信组件
        /// </summary>
        IMessenger imessenger { get; set; } = null;

       public  string mName;

        public   CmdLine(string name)
        {
            mName = name;
        }

        /// <summary>
        /// 回调方法
        /// </summary>
        /// <param name="msg"></param>
        public virtual  void CallBack(string msg)
        {
            throw new NotImplementedException();
        }

        public virtual void Dispose()
        {
            throw new NotImplementedException();
        }

        public virtual  string Execute(string input)
        {
            throw new NotImplementedException();
        }

        public void MessengerInit(IMessenger messenger)
        {
            if (imessenger == null)
            {
                messenger.Register<NotificationMessageAction<string>>(this, mName, ReceiveMsg);
                this.imessenger = messenger;
            }
        }

         void ReceiveMsg(NotificationMessageAction<string> msg)
        {
            string res= ReceiveMsg(msg.Notification);
            if (res != null)
                msg.Execute(res);

        }

        public virtual string   ReceiveMsg(string msg)
        {
            throw new NotImplementedException();
        }

        public  void SendMsg(string targetName,string msg)
        {
            imessenger.Send<NotificationMessageAction<string>>(new NotificationMessageAction<string>(msg, CallBack), targetName);
        }

    }
}
